<!-- 我们的日期查询 -->

<template>
  <WebSearch>
    <template #btn>
      <ParamSelect
        v-if="param.hasType"
        bind="type"
        :dict="DICT.TYPE"
        :autoSearch="false"
        noClear
        @change="changeType"
      />
      <BtnDownload @click="toExport" />
    </template>

    <ParamRadio bind="dateType" :dict="DICT.DATE" @change="changeDateType" />
    <ParamDateRange bind="startTime, endTime" history noClear @change="changeRange" />
  </WebSearch>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import OurSearch from '@admin/views/Maintain/Report/ReportWork/components/OurSearch'
import props from './props'

// ============================== 导出组件 ============================== //

export default CORE.extend(OurSearch, {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'OurSearchDate',

  /**
   * 属性注册 (抽取以便查阅)
   */
  props,

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    ...OurSearch.computed,

    /**
     * 页面
     */
    Page: () => 'Device',
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    ...OurSearch.methods,

    /**
     * 切换类型
     */
    changeType() {
      const { type, startTime, endTime } = this.param

      // 更新父组件的数据，记录类型和起止时间
      if (type === 1) {
        const today = CORE.timeNum(CORE.today())
        const end = endTime > today ? today : endTime
        CORE.merge(this.parentReport, { type, param: { startTime, endTime: end } })
      } else if (type === 2 || type === 3) {
        CORE.merge(this.parentReport, { type, param: { startTime, endTime } })
      } else {
        const monthBegin = CORE.timeNum(CORE.dateStr(startTime).slice(0, 7))
        const monthEnd = CORE.timeNum(monthBegin, 0, 0, 1) - 1000
        CORE.merge(this.parentReport, { type, param: { startTime: monthBegin, endTime: monthEnd } })
      }
    },

    /**
     * 导出
     */
    toExport() {
      const { type, startTime, endTime } = this.param
      const dateStr = `${CORE.dateStr(startTime)} 至 ${CORE.dateStr(endTime)}`
      const fileName = this.fileName
      const sheetName = this.sheetName
      const base64Left = this.canvasLeft.toDataURL('image/png').slice(22)
      const base64Right = this.canvasRight.toDataURL('image/png').slice(22)

      Object.assign(this.downParam, { type, dateStr, fileName, sheetName, base64Left, base64Right })

      this.startDownload()
    },
  },
})
</script>
